#include "zobjects_largenum.h"

static int Mpf2StringObject( struct ZlangRuntime *rt , mpf_t value , size_t n_digits , struct ZlangObject *str_obj )
{
	char					*dump_str = NULL ;
	int32_t					dump_str_len ;
	int					nret = 0 ;
	
	dump_str_len = gmp_asprintf( & dump_str , "%.*Ff" , n_digits , value ) ;
	if( dump_str_len < 0 )
		return -1;
	
	nret = CallRuntimeFunction_string_SetStringValue( rt , str_obj , dump_str , dump_str_len ) ;
	if( nret )
		return -2;
	
	free( dump_str );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_largedecimal_Set_double;
int ZlangInvokeFunction_largedecimal_Set_double( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	double					d ;
	
	CallRuntimeFunction_double_GetDoubleValue( rt , in1 , & d );
	mpf_set_d( largedecimal_direct_prop->value , d );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_largedecimal_Set_string;
int ZlangInvokeFunction_largedecimal_Set_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	char					*str = NULL ;
	int32_t					str_len ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & str , & str_len );
	mpf_set_str( largedecimal_direct_prop->value , str , 10 );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_largedecimal_ToString_int;
int ZlangInvokeFunction_largedecimal_ToString_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	int32_t					n_digits ;
	int					nret = 0 ;
	
	CallRuntimeFunction_int_GetIntValue( rt , in1 , & n_digits );
	
	nret = Mpf2StringObject( rt , largedecimal_direct_prop->value , (size_t)n_digits , out1 );
	if( nret )
		return nret;
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_largedecimal;
void *ZlangCreateDirectProperty_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_prop = NULL ;
	
	largedecimal_prop = (struct ZlangDirectProperty_largedecimal *)malloc( sizeof(struct ZlangDirectProperty_largedecimal) ) ;
	if( largedecimal_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( largedecimal_prop , 0x00 , sizeof(struct ZlangDirectProperty_largedecimal) );
	
	mpf_init( largedecimal_prop->value );
	
	return largedecimal_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_largedecimal;
void ZlangDestroyDirectProperty_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_direct_prop = GetObjectDirectProperty(obj) ;
	
	if( largedecimal_direct_prop )
	{
		mpf_clear( largedecimal_direct_prop->value );
		
		free( largedecimal_direct_prop );
	}
	
	return;
}

ZlangToStringFunction ZlangToString_largedecimal;
int ZlangToString_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *obj , struct ZlangObject **tostr_obj )
{
	struct ZlangDirectProperty_largedecimal	*largedecimal_direct_prop = GetObjectDirectProperty(obj) ;
	int					nret = 0 ;
	
	nret = Mpf2StringObject( rt , largedecimal_direct_prop->value , (size_t)6 , (*tostr_obj) );
	if( nret )
		return nret;
	
	return 0;
}

ZlangFromDataPtrFunction ZlangFromDataPtr_largedecimal;
int ZlangFromDataPtr_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *obj , void *value_ptr , int32_t value_len )
{
	struct ZlangDirectProperty_largedecimal	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	mpf_set( obj_direct_prop->value , *(mpf_t *)value_ptr );
	
	return 0;
}

ZlangGetDataPtrFunction ZlangGetDataPtr_largedecimal;
int ZlangGetDataPtr_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *obj , void **value_ptr , int32_t *value_len )
{
	struct ZlangDirectProperty_largedecimal	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	if( value_ptr )
		(*value_ptr) = & (obj_direct_prop->value) ;
	if( value_len )
		(*value_len) = sizeof(obj_direct_prop->value) ;
	return 0;
}

ZlangOperatorFunction ZlangOperator_largedecimal_PLUS_largedecimal;
int ZlangOperator_largedecimal_PLUS_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largedecimal	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largedecimal	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largedecimal	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpf_add( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largedecimal_MINUS_largedecimal;
int ZlangOperator_largedecimal_MINUS_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largedecimal	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largedecimal	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largedecimal	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpf_sub( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largedecimal_MUL_largedecimal;
int ZlangOperator_largedecimal_MUL_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largedecimal	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largedecimal	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largedecimal	*out = NULL ;
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpf_mul( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largedecimal_DIV_largedecimal;
int ZlangOperator_largedecimal_DIV_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largedecimal	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largedecimal	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largedecimal	*out = NULL ;
	
	if( mpf_get_ui(in2->value) == 0 )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "*** FATAL : division by zero" )
		return ThrowFatalException( rt , EXCEPTION_CODE_GENERAL , EXCEPTION_MESSAGE_DIVISION_BY_ZERO );
	}
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpf_div( out->value , in1->value , in2->value );
	
	return 0;
}

ZlangOperatorFunction ZlangOperator_largedecimal_MOD_largedecimal;
int ZlangOperator_largedecimal_MOD_largedecimal( struct ZlangRuntime *rt , struct ZlangObject *in_obj1 , struct ZlangObject *in_obj2 , struct ZlangObject **out_obj )
{
	struct ZlangDirectProperty_largedecimal	*in1 = GetObjectDirectProperty(in_obj1) ;
	struct ZlangDirectProperty_largedecimal	*in2 = GetObjectDirectProperty(in_obj2) ;
	struct ZlangDirectProperty_largedecimal	*out = NULL ;
	mpf_t					q , p ;
	
	if( fabs(mpf_get_d(in2->value)) <= DOUBLE_EPSILON )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "*** FATAL : division by zero" )
		return ThrowFatalException( rt , EXCEPTION_CODE_GENERAL , EXCEPTION_MESSAGE_DIVISION_BY_ZERO );
	}
	
	out = GetObjectDirectProperty(*out_obj) ;
	mpf_init( q );
	mpf_init( p );
	mpf_div( q , in1->value , in2->value );
	mpf_mul( p , q , in2->value );
	mpf_sub( out->value , in1->value , p );
	mpf_clear( q );
	mpf_clear( p );
	
	return 0;
}

static struct ZlangDirectFunctions direct_funcs_largedecimal =
	{
		ZLANG_OBJECT_largedecimal , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_largedecimal , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_largedecimal , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		ZlangToString_largedecimal , /* ZlangToStringFunction *to_string_func */
		ZlangFromDataPtr_largedecimal , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		ZlangGetDataPtr_largedecimal , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		ZlangOperator_largedecimal_PLUS_largedecimal , /* ZlangOperatorFunction *oper_PLUS_func */
		ZlangOperator_largedecimal_MINUS_largedecimal , /* ZlangOperatorFunction *oper_MINUS_func */
		ZlangOperator_largedecimal_MUL_largedecimal , /* ZlangOperatorFunction *oper_MUL_func */
		ZlangOperator_largedecimal_DIV_largedecimal , /* ZlangOperatorFunction *oper_DIV_func */
		ZlangOperator_largedecimal_MOD_largedecimal , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		NULL , /* ZlangSummarizeDirectPropertySizeFunction */
	} ;

ZlangImportObjectFunction ZlangImportObject_largedecimal;
struct ZlangObject *ZlangImportObject_largedecimal( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_largedecimal , & direct_funcs_largedecimal , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_largedecimal_Set_double , ZLANG_OBJECT_void , "Set" , ZLANG_OBJECT_double,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_largedecimal_Set_string , ZLANG_OBJECT_void , "Set" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_largedecimal_ToString_int , ZLANG_OBJECT_string , "ToString" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	mpf_set_default_prec( 256 );
	
	return obj;
}

